package com.zh.freechat.assemble;

import com.zh.freechat.common.req.ChatUserForm;
import com.zh.freechat.common.resp.ChatUserVO;
import com.zh.freechat.domain.user.ChatUserDTO;
import com.zh.freechat.domain.user.entity.ChatUser;
import org.springframework.beans.BeanUtils;

import java.util.Optional;

/**
 * @author ZH
 * @date 13:5 221/8/18
 */
public final class ChatUserWrapper {

    public static ChatUser wrapper(ChatUserForm form) {
        ChatUser user = ChatUser.createAndInitDefault();
        user.setUsername(form.getUsername());
        user.setNickname(Optional.ofNullable(form.getNickname()).orElse(form.getUsername()));
        user.setPassword(form.getPassword());
        user.setPhone(form.getPhone());
        user.setEmail(form.getEmail());
        return user;
    }

    public static ChatUserDTO wrapperDTO(ChatUser user) {
        ChatUserDTO userDto = new ChatUserDTO();
        BeanUtils.copyProperties(user,userDto);
        userDto.setUid(user.getUid());
        userDto.setPassword(null); //清除敏感信息
        return userDto;
    }

    public static ChatUserVO convertVO(ChatUser user) {
        ChatUserVO vo = new ChatUserVO();
        BeanUtils.copyProperties(user,vo);
        return vo;
    }

}
